package com.mall.model.vo;

import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

import static com.mall.common.util.Constants.*;

/**
 * @author : xyiaao
 * @createDate : 2022/3/30 9:47
 */
@Data
@NoArgsConstructor
public class CommonResult<T> implements Serializable {

    private Long code;

    private T data;

    private String message;

    public static CommonResult success() {

        CommonResult commonResult = new CommonResult<>();
        commonResult.code = SUCCESS_CODE;
        commonResult.data = SUCCESS_DATA;
        commonResult.message = SUCCESS_MESSAGE;
        return commonResult;
    }

    public static <E> CommonResult<E> success(E data) {

        CommonResult<E> commonResult = new CommonResult<>();
        commonResult.data = data;
        commonResult.code = SUCCESS_CODE;
        commonResult.message = SUCCESS_MESSAGE;
        return commonResult;
    }

    public static <E> CommonResult<E> success(String message, E data) {

        CommonResult<E> commonResult = new CommonResult<>();
        commonResult.data = data;
        commonResult.code = SUCCESS_CODE;
        commonResult.message = message;
        return commonResult;
    }


    public static <E> CommonResult<E> error(String message) {

        CommonResult<E> commonResult = new CommonResult<>();
        commonResult.code = ERROR_CODE;
        commonResult.message = message;
        return commonResult;
    }

    public static <E> CommonResult<E> error(String message, E data) {

        CommonResult<E> commonResult = new CommonResult<>();
        commonResult.data = data;
        commonResult.code = 1L;
        commonResult.message = message;
        return commonResult;
    }

    public static CommonResult error(Integer code, String message) {

        CommonResult commonResult = new CommonResult();
        commonResult.message = message;
        commonResult.code = Long.valueOf(code);
        return commonResult;
    }

    public static <E> CommonResult<E> error(Integer code, String message, E data) {

        CommonResult<E> commonResult = new CommonResult<>();
        commonResult.data = data;
        commonResult.code = Long.valueOf(code);
        commonResult.message = message;
        return commonResult;
    }

}
